package com.cmy.framework.mvvm.binding.viewadapter.image

import android.text.TextUtils
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import coil.load
import coil.transform.CircleCropTransformation

/**
 * @name:
 *  ViewAdapter
 * @package:
 *  com.cmy.framework.mvvm.binding.viewadapter.image
 * @date:
 *  2023/3/211:23
 * @author
 *    cmy
 * @description
 *
 */
object ViewAdapter {
        @BindingAdapter(value = ["url", "placeholderRes"], requireAll = false)
        @JvmStatic
        fun setImageUri(imageView: ImageView?, url: String?, placeholderRes: Int?) {
            if (!TextUtils.isEmpty(url)) {
                //使用Glide框架加载图片
                imageView?.load(url){
                    crossfade(true) //淡入淡出
                    crossfade(1000)//设置显示动画的时间
                    placeholderRes?.let {
                        error(placeholderRes)//图片加载失败时显示的图
                        if (placeholderRes > 0) {
                            placeholder(placeholderRes)
                        }else{
                            placeholder(com.cmy.framework_base.R.drawable.base_empty)
                        }
                    }


                }
            } else {
                placeholderRes?.let {
                    imageView?.setImageResource(placeholderRes)
                }
            }
        }


    @BindingAdapter(value = ["circleImageUrl"])
    @JvmStatic
    fun circleImageUrl(view: ImageView, url: String, placeholderRes: Int) {
        view.load(url) {

            crossfade(true) //淡入淡出
            crossfade(1000)//设置显示动画的时间
//                transition()
            error(placeholderRes)//图片加载失败时显示的图
            if (placeholderRes > 0) {
                placeholder(placeholderRes)
            }else{
                placeholder(com.cmy.framework_base.R.drawable.base_empty)
            }
            CircleCropTransformation()//图片变换，将图片转为圆形
        }
    }


    @BindingAdapter(value = ["roundedImageUrl"], requireAll = true)
    @JvmStatic
    fun roundedImageUrl(view: ImageView, url: String, placeholderRes: Int){
        view.load(url) {
            crossfade(true) //淡入淡出
            crossfade(1000)//设置显示动画的时间
//                transition()
            error(placeholderRes)//图片加载失败时显示的图
            if (placeholderRes > 0) {
                placeholder(placeholderRes)
            }else{
                placeholder(com.cmy.framework_base.R.drawable.base_empty)
            }
            CircleCropTransformation()//图片变换，将图片转为圆形
        }
    }


}